/* @(#)GSLoginAction.java    
 *
 * Project: BUS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Simonyi Peng 2006-2-7    建立
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.util.ArrayList;

import system.LoginContext;
import com.hitrust.gs.business.GSOrderProcesser;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.form.GSBusOrderCancelListForm;
import com.hitrust.gs.form.GSOrderSubmitForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.exception.*;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSBusOrderCancelVO;
import com.hitrust.gs.vo.GSOrderAuditQueryVO;

public class GSProductStroageQueryAction extends GSBaseAction {
    private GSOrderProcesser ordProcesser = new GSOrderProcesser();
    
    public String execute(GSBaseForm aForm) throws GSException {
        GSOrderSubmitForm tForm = (GSOrderSubmitForm) aForm;
        /**         获得查询条件                 */
        GSOrderAuditQueryVO oaqVO = new GSOrderAuditQueryVO();
        oaqVO.deptId = tForm.getOrderDept();
        oaqVO.concentricityId = tForm.getConcentricityId();
        oaqVO.manufInfo = tForm.getManufInfo();
        oaqVO.showMinZero = tForm.getShowMinZero();
        /**         获得查询条件                 */
        
        if (aForm.getRequest().getAttribute("ISQUERYCONDITIONEXIST") != null) {
            // 是执行完返回的，读出查询条件
            oaqVO = (GSOrderAuditQueryVO) aForm.getSession().getAttribute("QUERYCONDITION");
        } else {
            // 初次查询，缓存查询条件
            aForm.getSession().setAttribute("QUERYCONDITION", oaqVO);
        }
        try {
            String[] tStr = this.getPageParams(tForm,oaqVO);
            tForm.getRequest().setAttribute("pageSize",tStr[1]);
            tForm.getRequest().setAttribute("totalRecords",tStr[2]);
            ArrayList orderAry = ordProcesser.getProductSotrageViewList(oaqVO, tStr[0], tStr[1]);
            ResultProcess tRsProc = new ResultProcess();
            tRsProc.setResult(orderAry);
            tForm.getRequest().setAttribute("orderlist", tRsProc);
            tForm.getRequest().getSession().setAttribute("oaqVO",oaqVO);
            LoginContext loginContext = (LoginContext)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
            GSUniteGwProcessor processer = new GSUniteGwProcessor();
            ArrayList ary = processer.getWareHouseLevelName(loginContext.getLoginWarehouseCode(),tForm.getOrderDept());
            aForm.getRequest().setAttribute("organdaddr", ary);
            aForm.getRequest().setAttribute("showMinZero", oaqVO.showMinZero);
        } catch (GSException e) {
            aForm.getRequest().setAttribute("action", "A102001.do");
            throw e;
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            tForm.getRequest().setAttribute("action", "A102001.do");
            throw new GSException(GSException.E20054, GSException.E20054_MSG);
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }
    private String[] getPageParams(GSOrderSubmitForm tForm,GSOrderAuditQueryVO vo) throws Exception{
        String tStr[] = new String[3];
        int totalRecords;
        int pageNo;
        int pageSize;
        String tPage = GSBaseConstants.tPage;
        //根据页面传入的查询参数查询订单信息
        if(tForm.getChoice() == null || tForm.getChoice().equals("")){
            totalRecords = ordProcesser.getProductSotrageCount(vo);
            
            // 取得系统默认的页大小
            pageSize = Integer.parseInt(tPage);
            // PageNo=1
            pageNo = 1;
            int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
            if (pageNo>n) {
                pageNo=n;
            }
            tStr[0] = "" + pageNo;
            tStr[1] = "" + pageSize;
            tStr[2] = "" + totalRecords;
        }else{
            tStr = GSBaseManager.getPageNoAndPageSize(tForm);
        }
        return tStr;
    }
}
